$(function () {
    // window.open('http://124.239.175.119:8089/yueke/login.aspx');

    //初始化数据
    sendMessageToContentScript({ cmd: 'initData' }, function (response) {
        if (response.km) {
            $("#km").find("option[value = '" + response.km + "']").attr("selected", "selected");
        }

        if (response.ks) {
            $("#ks").find("option[value = '" + response.ks + "']").attr("selected", "selected");
        }

        if (response.timer != null && response.timer != 'undefined') {
            $("#timer").val(response.timer);
        }
        else {
            $("#timer").val('1');
        }
    });



    $("#btnSave").on("click", function () {
        if (!$("#km").val()) {
            $("#msg").html("科目不能为空!");
            return;
        }

        if (!$("#ks").val()) {
            $("#msg").html("课时不能为空!");
            return;
        }

        if (!$("#timer").val()) {
            $("#msg").html("刷新间隔不能为空!");
            return;
        }

        //赋值
        sendMessageToContentScript({ cmd: 'save', km: $("#km").val(), ks: $("#ks").val(), timer: $("#timer").val() }, function (result) {
            $("#msg").html(result);
            this.close();
        });
    });
});

//给content发送信息
function sendMessageToContentScript(message, callback) {
    chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
        chrome.tabs.sendMessage(tabs[0].id, message, function (response) {
            if (callback) callback(response);
        });
    });
}

